﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebGioiThieu.Models;
using System.Xml.Serialization;

namespace WebGioiThieu.Controllers
{
    public class TourController : BaseController
    {
        private IntroductionWebDBEntities db = new IntroductionWebDBEntities();

        // GET: /Tour/
        public ViewResult Index(string slug)
        {
            listMenuItem = new List<int>();
            MenuItem menuItem = GetMenuItemBySlug(slug);
            listMenuItem.Add(menuItem.ID);
            if (menuItem != null)
            {
                GetChildMenuItem(menuItem.ID);
                List<Tour> tour = GetTourByMenuItemID();
                return View(tour);
            }
            else
            {
                return View("NotFound");
            }
        }
        private static List<int> listMenuItem = null;
        private static MenuItem GetMenuItemBySlug(string slug)
        {
            IntroductionWebDBEntities dbb = new IntroductionWebDBEntities();
            return dbb.MenuItems.Single(i => i.BiDanh == slug);
        }
        private static void GetChildMenuItem(int id)
        {
            IntroductionWebDBEntities dbb = new IntroductionWebDBEntities();
            List<MenuItem> l = dbb.MenuItems.Where(i => i.Parent == id).ToList();
            foreach (MenuItem i in l)
            {
                listMenuItem.Add(i.ID);
                GetChildMenuItem(i.ID);
            }
            return;
        }
        private static List<Tour> GetTourByMenuItemID()
        {
            IntroductionWebDBEntities dbb = new IntroductionWebDBEntities();
            List<Tour> t = new List<Tour>();
            foreach (int m in listMenuItem)
            {
                List<Tour> temp = dbb.Tours.Where(i => i.MenuItemID == m).Where(i => i.IsTour == true).Where(i => i.TrangThai == true).ToList();
                if (temp != null)
                    t.AddRange(temp);
            }
            return t;

        }
        //
        // GET: /Tour/Details/5

        public ViewResult Details(int id)
        {
            Tour tour = db.Tours.Find(id);
            ViewBag.getComment = db.Comments.Where(c=>c.Publish==true).Where(c => c.TourID == id).ToList();
            ViewBag.comment = new Comment();
            if (ModelState.IsValid)
            {
                tour.LuotXem += 1;
                db.Entry(tour).State = EntityState.Modified;
                db.SaveChanges();
            }
            return View(tour);
        }
        [ChildActionOnly]
        public ActionResult ViewFeature()
        {
            var feature = db.Tours.Where(i => i.IsTour == true).Where(i => i.TrangThai == true).Where(t => (t.NoiBat == true)).Select(t => new TourModels
            {
                ID = t.ID,
                TieuDe = t.TieuDe,
                PhanModau = t.PhanModau,
                HinhAnh = t.HinhAnh,
                Count = t.LuotXem
            }).Take(4).ToList();
            return PartialView(feature);
        }

        [ChildActionOnly]
        public ActionResult TopView()
        {
            var feature = db.Tours.Where(i => i.IsTour == true).Where(i => i.TrangThai == true).OrderByDescending(t => t.LuotXem)
                .Select(t => new TourModels
                {
                    ID = t.ID,
                    TieuDe = t.TieuDe,
                    PhanModau = t.PhanModau,
                    HinhAnh = t.HinhAnh,
                    Count = t.LuotXem
                }).Take(4).ToList();
            return PartialView(feature);
        }
       
        //
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}